package fr.umlv.m2.jee.spij.web.file.upload;

import org.ajax4jsf.exception.FileUploadException;
import org.richfaces.event.UploadEvent;
import org.richfaces.model.UploadItem;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * Bean use to manage a file upload
 *
 * @author Christophe Chillan
 */
@Component("fileUploadBean")
public class FileUploadBean {

    private List<FileUploaded> fileUploadeds = new ArrayList<FileUploaded>();

    private int uploadsAvailable = 1;

    private boolean autoUpload = false;

    private boolean useFlash = false;

    private byte[] datas;

    /**
     * Get the number of the file which has already been uploaded
     *
     * @return the of the files uploaded
     */
    public int getSize() {
        return fileUploadeds.size();
    }

    public FileUploadBean() {
    }

    /**
     * Define the listener uses in the file upload page
     *
     * @param event .
     *
     * @throws Exception
     */
    public void listener(UploadEvent event) throws Exception {
        UploadItem item = event.getUploadItem();

        FileUploaded fileUploaded = new FileUploaded();
        try {
            byte[] data = item.getData();

            fileUploaded.setLength(data.length);
            fileUploaded.setName(item.getFileName());
            datas = data;
            fileUploadeds.add(fileUploaded);
            uploadsAvailable--;
        } catch (FileUploadException e) {
            e.printStackTrace();
        }


    }

    /**
     * Clear the data uploaded
     *
     * @return clearOK if the data has been deleted
     */
    public String clearUploadData() {
        fileUploadeds.clear();
        setUploadsAvailable(uploadsAvailable);
        return null;
    }

    /**
     * get all the uploaded files
     *
     * @return the list of the uploaded files
     */
    public List<FileUploaded> getFileUploadeds() {
        return Collections.unmodifiableList(fileUploadeds);
    }


    /**
     * Set the uploaded files
     *
     * @param FileUploadeds
     */
    public void setFileUploadeds(List<FileUploaded> FileUploadeds) {
        this.fileUploadeds = new ArrayList<FileUploaded>(FileUploadeds);
    }

    /**
     * get the number of available upload
     *
     * @return the number of the remaining upload
     */
    public int getUploadsAvailable() {
        return uploadsAvailable;
    }

    /**
     * Set the number of the available upload
     *
     * @param uploadsAvailable
     */
    public void setUploadsAvailable(int uploadsAvailable) {
        this.uploadsAvailable = uploadsAvailable;
    }

    /**
     * Define if the upload is automatic
     *
     * @return true if the upload start when the file is chosen, false otherwise
     */
    public boolean isAutoUpload() {
        return autoUpload;
    }

    /**
     * set if the file upload is automatic
     *
     * @param autoUpload
     */
    public void setAutoUpload(boolean autoUpload) {
        this.autoUpload = autoUpload;
    }

    /**
     * Define if the flash option is activate
     *
     * @return true if the file upload use flash, false otherwise
     */
    public boolean isUseFlash() {
        return useFlash;
    }

    /**
     * Set the flash option
     *
     * @param useFlash
     */
    public void setUseFlash(boolean useFlash) {
        this.useFlash = useFlash;
    }

    /**
     * @return
     */
    public byte[] getFileData() {
        fileUploadeds.clear();
        return Arrays.copyOf(datas, datas.length);
    }
}
